<?php


namespace app\common\service;


use app\web\constant\CommonConstant;
use think\Db;

/**
 * 会员卡服务
 *
 * Class ViperCardService
 * @package app\common\service
 */
class ViperCardService {


    /**
     * 获取月卡模版
     */
    public static function getMonthTemplate ($store_id,$template_id=0) {
        $where = [];

        if (!empty($template_id)) {
            $where = ['template.id'=>$template_id];
        }
        $template_item_data = Db::name('qt_viper_month_card_template_item')
            ->alias('item')
            ->leftJoin('qt_viper_month_card_template template','template.id=item.month_template_id')
            ->where('template.store_id',$store_id)
            ->where($where)
            ->select();

        foreach ($template_item_data as $key => &$item) {
            if ($item['item_type'] == 'goods') {
                $item['item_list'] = Db::name('qt_goods')->where('id', $item['item_id'])->select();
            }
            if ($item['item_type'] == 'project') {
                $item['item_list'] = Db::name('qt_project')->where('id', $item['item_id'])->select();
            }
        }
        return $template_item_data;
    }


    /**
     * 获取次卡模版
     */
    public static function getNumberTemplate ($store_id,$template_id=0) {

        $where = [];
        if (!empty($template_id)) {
            $where = ['template.id'=>$template_id];
        }
        $template_item_data = Db::name('qt_viper_number_card_template_item')
            ->field('item.*,template.name')
            ->alias('item')
            ->leftJoin('qt_viper_number_card_template template','item.number_card_template_id=template.id')
            ->where('template.store_id',$store_id)
            ->where($where)
            ->select();
        foreach ($template_item_data as $key => &$item) {
            if ($item['item_type'] == 'goods') {
                $item['item_list'] = Db::name('qt_goods')->where('id', $item['item_id'])->select();
            }
            if ($item['item_type'] == 'project') {
                $item['item_list'] = Db::name('qt_project')->where('id', $item['item_id'])->select();
            }
        }

        return $template_item_data;
    }


    /**
     * 获取普通充值模版
     */
    public static function getTemplate ($store_id,$recharge_temp_id=0) {

        $where = [];
        if (!empty($recharge_temp_id)) {
            $where[] = ['id','=',$recharge_temp_id];
        }
        $template = Db::name('qt_viper_recharge_template')
            ->where('store_id',$store_id)
            ->where($where)
            ->select();
        return $template;
    }



}